package se.sll.service.catalogue.notif.test;

import java.util.Enumeration;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;
import lombok.extern.slf4j.Slf4j;
import se.sll.service.catalogue.common.property.CatalogProperties;

/**
 * This MDB is for test purposes only.
 * @author Jiri Uosukainen
 */
@Slf4j
//@MessageDriven(mappedName= CatalogProperties.JMS_DLQ_JNDI)
public class EiDlqListenerMDB implements MessageListener{
    
    @Override
    public void onMessage(Message jmsMessage){
        log.info("onMessage(): ENTER");

        log.info("EiDlqListenerMDB is mapped to QUEUE : {}", CatalogProperties.JMS_DLQ_JNDI);
        
        try {
            Enumeration propNames = jmsMessage.getPropertyNames();
            while (propNames.hasMoreElements()) {
                String propName = (String)propNames.nextElement();
                log.info("JMS message property: {}={}", new Object[]{propName, jmsMessage.getStringProperty(propName)} );
            }
            
            if(jmsMessage instanceof TextMessage){
                String contents = ((TextMessage)jmsMessage).getText();
                log.info("JMS message class: [{}]", contents.getClass().getName());
                log.info("JMS message contents: [{}]", new Object[]{contents} );
            }

            if(jmsMessage instanceof ObjectMessage){
                Object msg = ((ObjectMessage)jmsMessage).getObject();
                log.info("JMS message class: [{}]", msg.getClass().getName());
                log.info("JMS message contents: [{}]", msg.toString());
            }
            
        }
        catch (JMSException ex) {
            log.error("Caught JMSException: {}", new Object[]{ex.getMessage()});
        }
        
        log.info("onMessage(): EXIT");
    }
}
